//
//  FirstViewController.m
//  Socrates
//
//  Created by shy on 15/3/2.
//  Copyright (c) 2015年 shy. All rights reserved.
//

#import "HomeViewController.h"
#import "LoginViewController.h"
#import "HomeCell.h"
#import "ActivityDetailViewController.h"
#import "UserInfoViewController.h"
#import "HomeCellDelegate.h"
typedef enum {
    kMe_about_us_item = 0,
    kMe_upgrade_version_item,
    kMe_share_friends_item,
    kLimitation_Num,
} MeItems;

static const int kLimitationRowHeight = 180;

@interface HomeViewController ()<HomeCellDelegate>
@property (strong, nonatomic) UIBarButtonItem *homeRightBarBtn;
@property (strong, nonatomic) NSMutableArray *indexesofLike;//纪录点赞的index
@end

@implementation HomeViewController
#pragma mark - view Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"首页";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    self.navigationItem.rightBarButtonItem = self.homeRightBarBtn;
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeCell" bundle:nil] forCellReuseIdentifier:@"HomeCell"];
}

- (void)viewWillAppear:(BOOL)animated{
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - tableView Datasource

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
    view.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kLimitationRowHeight;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return kLimitation_Num;
}

- (HomeCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"HomeCell";
    HomeCell *cell           = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.HomeCellDelagate    = self;
    cell.currntIndex         = indexPath;
    [cell.userImage addTarget:self action:@selector(clickUser) forControlEvents:UIControlEventTouchUpInside];
    if ([self.indexesofLike containsObject:[NSNumber numberWithInteger:indexPath.row]]) {
        [cell.likeImage setImage:[UIImage imageNamed:@"profile_btn_like"] forState:UIControlStateNormal];
    }
    else{
        [cell.likeImage setImage:[UIImage imageNamed:@"profile_btn_unlike"] forState:UIControlStateNormal];
    }
    
    switch ([indexPath row]) {
        case kMe_about_us_item:
            cell.activityName.text = @"周杰伦2015个人演唱会";
            cell.activityFee.text  = @"288/人";
            cell.declareTime.text  = @"1小时前";
            cell.time.text         = @"2015/11/28 19:00";
            cell.location.text     = @"北京 朝阳区 鸟巢体育馆";
            cell.numMale.text      = @"11/18";
            cell.numFemale.text    = @"12/21";
            cell.userName.text     = @"AngelaBaby";
            cell.userAge.text      = @"21";
            cell.userSchool.text   = @"对外经贸大学";
            cell.likes.text        = @"25";
            break;
        case kMe_upgrade_version_item:
            cell.activityName.text = @"周杰伦2015个人演唱会";
            cell.activityFee.text  = @"288/人";
            cell.declareTime.text  = @"1小时前";
            cell.time.text         = @"2015/11/28 19:00";
            cell.location.text     = @"北京 朝阳区 鸟巢体育馆";
            cell.numMale.text      = @"11/18";
            cell.numFemale.text    = @"12/21";
            cell.userName.text     = @"AngelaBaby";
            cell.userAge.text      = @"21";
            cell.userSchool.text   = @"对外经贸大学";
            cell.likes.text        = @"25";
            break;
        case kMe_share_friends_item:
            cell.activityName.text = @"周杰伦2015个人演唱会";
            cell.activityFee.text  = @"288/人";
            cell.declareTime.text  = @"1小时前";
            cell.time.text         = @"2015/11/28 19:00";
            cell.location.text     = @"北京 朝阳区 鸟巢体育馆";
            cell.numMale.text      = @"11/18";
            cell.numFemale.text    = @"12/21";
            cell.userName.text     = @"AngelaBaby";
            cell.userAge.text      = @"21";
            cell.userSchool.text   = @"对外经贸大学";
            cell.likes.text        = @"25";
            break;
        default:
            break;
    }
    return cell;
}
#pragma mark - tableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    ActivityDetailViewController *vc = [[ActivityDetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"click for detail");
}
#pragma mark - HomeCellDelegate
-(void)didTouchLike:(NSIndexPath*)index{
    if ([self.indexesofLike containsObject:[NSNumber numberWithInteger:index.row]]) {
        [self.indexesofLike removeObject:[NSNumber numberWithInteger:index.row]];
        [((HomeCell*)[self.tableView cellForRowAtIndexPath:index]).likeImage setImage:[UIImage imageNamed:@"profile_btn_unlike"] forState:UIControlStateNormal];
        CAKeyframeAnimation *k = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        k.values = @[@(0.1),@(1.0),@(1.5)];
        k.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
        k.calculationMode = kCAAnimationLinear;
        k.delegate = self;
        [((HomeCell*)[self.tableView cellForRowAtIndexPath:index]).likeImage.layer addAnimation:k forKey:@"SHOW"];
    }
    else{
        [self.indexesofLike addObject:[NSNumber numberWithInteger:index.row]];
        [((HomeCell*)[self.tableView cellForRowAtIndexPath:index]).likeImage setImage:[UIImage imageNamed:@"profile_btn_like"] forState:UIControlStateNormal];
        CAKeyframeAnimation *k = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        k.values = @[@(0.1),@(1.0),@(1.5)];
        k.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
        k.calculationMode = kCAAnimationLinear;
        k.delegate = self;
        [((HomeCell*)[self.tableView cellForRowAtIndexPath:index]).likeImage.layer addAnimation:k forKey:@"SHOW"];
    }
}
#pragma mark - public Method

#pragma mark - private Method(正常情况下应该有一个helper class代替)
/**
 *  获取homecell
 *
 *  @param tempData  带传入数据，应为模型类
 *  @param indexPath 当前cell的index
 *  @param reuse     是否已经点赞
 *
 *  @return 当前cell
 */
-(HomeCell*)getHomeCellwithData:(id*)tempData andIndex:(NSIndexPath*)indexPath andReuse:(BOOL)reuse{
    return nil;
}
#pragma mark - event Response
- (void)clickRightBarButton:(id)sender {
    
}

- (void)clickUser {
    NSLog(@"have react.");
    UserInfoViewController *vc = [[UserInfoViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - setter and getter
- (UIBarButtonItem *)homeRightBarBtn {
    if (_homeRightBarBtn == nil) {
        UIButton *settings = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 20)];
        [settings setBackgroundImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        [settings addTarget:self action:@selector(clickRightBarButton:) forControlEvents:UIControlEventTouchUpInside];
        _homeRightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:settings];
    }
    return _homeRightBarBtn;
}
- (NSMutableArray*)indexesofLike{
    if (_indexesofLike == nil) {
        _indexesofLike = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _indexesofLike;
}
@end
